package com.netrust.quarz.aspect;

import org.apache.lucene.util.RamUsageEstimator;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LogAspect {
	
	private static final Logger logger = LoggerFactory.getLogger(LogAspect.class);
	
	
	 	@Pointcut("execution(public * com.netrust.quarz.service.*.*(..))")//要处理的方法，包名+类名+方法名
	    public void cut(){
	    }

	    @Before("cut()")//在调用上面 @Pointcut标注的方法前执行以下方法
	    public void doBefore(JoinPoint joinPoint){//用于获取类方法
	        logger.info("执行>>>>>> {}",joinPoint.getSignature().getDeclaringTypeName()+'.'+ joinPoint.getSignature().getName());
	       
	    }
	    
	    @After("cut()")
	    public void doAfter(){
	        logger.info(">>>>>>完成service调用>>>>>>");
	    }

	    
	    @AfterThrowing(pointcut = "cut()",throwing = "e")//切点异常【抛出
	    public void doAfterThrowing(JoinPoint joinPoint,  Throwable e){
	    	logger.error("异常>>>> {}",e);
	    }
	    
	    @AfterReturning(returning = "obj",pointcut = "cut()")//在调用上面 @Pointcut标注的方法后执行。用于获取返回值
	    public void doAfterReturning(Object obj){
	    	
	    	if(obj==null){
	    		logger.info("返回>>>> {}","null");	    		
	    	}else{
	    		long key=RamUsageEstimator.sizeOf(obj.toString().getBytes());

	    		if(key>30720){
	    			logger.info("返回>>>> {}",obj.getClass());
	    		}
	    		logger.info("返回>>>> {}",obj.toString());
	    	}
	    }
}
